package com.zx.cloud.common.qiniu;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.zx.cloud.config.ConfigUtil;
import com.zx.cloud.security.EncodeUtil;

@Component
public class MagicUploader {

	private static int counter = 0;
	@Autowired
	private ConfigUtil configUtil;
	private Auth auth;
	private UploadManager uploadManager;
	private BucketManager bucketManager;

	@PostConstruct
	public void init() {
		auth = Auth.create(configUtil.getQiNiuAccessKey(), configUtil.getQiNiuSecretKey());
		uploadManager = new UploadManager();
		bucketManager = new BucketManager(auth);
	}

	private String getUpToken() {
		return auth.uploadToken(configUtil.getQiNiuBucketname());
	}

	private String encodingFilename(String filename) {
		return EncodeUtil.encodeMD5(filename + System.nanoTime() + counter++) + "." + filename;
	}

	public boolean delete(String filePath) {
		String sufixString = null;
		int slashIndex = filePath.indexOf(".");
		if (slashIndex >= 0) {
			sufixString = filePath.substring(slashIndex + 1);
		}
		String filename = encodingFilename(sufixString);
		try {
			bucketManager.delete(configUtil.getQiNiuBucketname(), filename);
			return true;
		} catch (QiniuException e) {
			Response r = e.response;
			System.out.println(r.toString());
		}
		return false;
	}
	
	public String send(String filename_,byte[] data) {
		System.err.println("-------QiNiuAccessKey------"+configUtil.getQiNiuAccessKey());
		System.err.println("-------QiNiuSecretKey------"+configUtil.getQiNiuSecretKey());
		
		String sufixString = null;
		int slashIndex = filename_.indexOf(".");
		if (slashIndex >= 0) {
			sufixString = filename_.substring(slashIndex + 1);
		}
		String filename = encodingFilename(sufixString);
		try {
			Response res = uploadManager.put(data,filename, getUpToken());
			StringMap stringMap = res.jsonToMap();
			return configUtil.getQiNiuAccessUrl() + "/" + stringMap.get("key").toString();
		} catch (QiniuException e) {
			e.printStackTrace();
			System.out.println(e.response.toString());
		}
		return null;
	}
	
	public String send(String filename_,FileType fileType,Long userId ,byte[] data) {
		String sufixString = null;
		int slashIndex = filename_.indexOf(".");
		if (slashIndex >= 0) {
			sufixString = filename_.substring(slashIndex + 1);
		}
		String filename = encodingFilename(sufixString);
		try {
			Response res = uploadManager.put(data,userId +"_"+ fileType.getCode()+"_"+filename, getUpToken());
			StringMap stringMap = res.jsonToMap();
			return configUtil.getQiNiuAccessUrl() + "/" + stringMap.get("key").toString();
		} catch (QiniuException e) {
			e.printStackTrace();
			System.out.println(e.response.toString());
		}
		return null;
	}

}
